package nz.co.iswe.android.airplay;

import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jmdns.JmDNS;

/**
 * Created by Admin on 2016/5/5.
 */
public class AirPlayServerTwo implements Runnable{

    /**
     * 服务类型
     */
    static final String AIR_PLAY_SERVICE_TYPE = "_airplay._tcp.local.";//AirPlay服务类型（视频 图片）

    /**
     * The AirTunes/RAOP M-DNS service properties (TXT record)
     * 文档记录
     */
    static final Map<String, String> AIRTUNES_SERVICE_PROPERTIES = map(
            "name"," Apple TV",
            "type","_airplay._tcp",
            "port","7000",
            "deviceid","58:55:CA:1A:E2:88",
            "features","0x39f7",
            "srcvers","130.14"
    );

    private static AirPlayServerTwo instance = null;

    public AirPlayServerTwo getInstance(){
        if( instance == null){
            instance = new AirPlayServerTwo();
        }
        return instance;
    }

    //初始化服务
    private ExecutorService executorService;

    //多线程扩展通道
    protected ExecutionHandler channelExecutionHandler;

    //关闭关闭challens
    protected ChannelGroup channelGroup;

    //用于注销mDNS
    protected List<JmDNS> jmDNSInstances;

    //接口
    private int rtspPort = 8000;

    private AirPlayServerTwo(){
        //create executor service创建执行服务
        executorService = Executors.newCachedThreadPool();

        //create channel execution handler
        channelExecutionHandler = new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(4, 0, 0));

        //channel group
        channelGroup = new DefaultChannelGroup();

        //list of mDNS services
        jmDNSInstances = new java.util.LinkedList<JmDNS>();
    }

    public int getRtspPort() {
        return rtspPort;
    }

    public void setRtspPort(int rtspPort) {
        this.rtspPort = rtspPort;
    }








    @Override
    public void run() {

    }

    /**
     * Map factory. Creates a Map from a list of keys and values
     * 从一个keys和values的列表中创建一个映射
     *
     * @param keys_values key1, value1, key2, value2, ...
     * @return a map mapping key1 to value1, key2 to value2, ...
     */
    private static Map<String, String> map(final String... keys_values) {
        assert keys_values.length % 2 == 0;
        final Map<String, String> map = new java.util.HashMap<String, String>(keys_values.length / 2);
        for(int i=0; i < keys_values.length; i+=2)
            map.put(keys_values[i], keys_values[i+1]);
        return Collections.unmodifiableMap(map);
    }
}
